package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.tobato.fastdfs.proto.mapper.FdfsColumn;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.utils.FdfsUtil;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import lombok.SneakyThrows;
import org.apache.commons.collections4.EnumerationUtils;
import org.bouncycastle.crypto.agreement.jpake.JPAKERound1Payload;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVO findProductList(Integer pageNum, Integer pageSize, ProductVO productVO) {
        PageHelper.startPage(pageNum,pageSize);//开启分页
        List<Product> products;
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        if (productVO.getStatus()!=null){
            criteria.andEqualTo("status",+productVO.getStatus());
        }
        if (productVO.getThreeCategoryId() !=null){
            criteria.andEqualTo("oneCategoryId", productVO.getOneCategoryId())
                    .andEqualTo("twoCategoryId", productVO.getTwoCategoryId())
                    .andEqualTo("threeCategoryId", productVO.getThreeCategoryId());
            products = productMapper.selectByExample(example);
            List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
            PageInfo<Product> productPageInfo = new PageInfo<>(products);
            long total = productPageInfo.getTotal(); // 通过查询到的数据，用PageInfo 获取他的总记录数total ，PageVO要用
            PageVO<ProductVO> pageVO = new PageVO<>(total, productVOS);
            return pageVO;
        }
        if(productVO.getTwoCategoryId()!=null){
            criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
                    .andEqualTo("twoCategoryId",productVO.getTwoCategoryId());
            products = productMapper.selectByExample(example);
            List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
            PageInfo<Product> productPageInfo = new PageInfo<>(products);
            long total = productPageInfo.getTotal();
            PageVO<ProductVO> pageVO = new PageVO<>(total, productVOS);
            return pageVO;
        }
        if (productVO.getOneCategoryId()!=null){
            criteria.andEqualTo("oneCategoryId" , productVO.getOneCategoryId());
            products = productMapper.selectByExample(example);
            List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
            PageInfo<Product> productPageInfo = new PageInfo<>(products);
            long total = productPageInfo.getTotal();
            PageVO<ProductVO> pageVO = new PageVO<>(total, productVOS);
            return pageVO;
        }
        example.setOrderByClause("sort ASC");
        if (productVO.getName()!=null && !"".equals(productVO.getName())) {
            criteria.andLike("name", "%"+productVO.getName()+"%");
        }
        //根据条件查询查询的结果
        products = productMapper.selectByExample(example);
        //将对象的List集合转换成VOList集合
        List<ProductVO> productVOS = ProductConverter.converterToVOList(products);
        //获取pageInfo 的对象  获取total
        PageInfo<Product> productPageInfo = new PageInfo<>(products);
        //total总记录数
        long total = productPageInfo.getTotal();
        PageVO<ProductVO> pageVO = new PageVO<>(total, productVOS);
        return pageVO;
    }

    //添加物资
    @Override
    public void add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO , product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        @NotNull(message = "分类不能为空") Long[] categoryKeys = productVO.getCategoryKeys();
        if (categoryKeys.length == 3){
            product.setOneCategoryId(categoryKeys[0]);
            product.setTwoCategoryId(categoryKeys[1]);
            product.setThreeCategoryId(categoryKeys[2]);
        }
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString().substring(0,33));
        productMapper.insert(product);
    }

    //根据ID 回显数据
    @Override
    public ProductVO edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product , productVO);
        return productVO;
    }

    //修改
    @Override
    public void update(Long id , ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO , product);
        product.setModifiedTime(new Date());
        productMapper.updateByPrimaryKey(product);
    }

    //移入回收站  修改状态值 回收站:1:逻辑删除,0:正常数据,2:添加待审核
    @Override
    public void remove(Long id) throws BusinessException {
        //根据Id进行查询数据
        Product product = productMapper.selectByPrimaryKey(id);
        if (product.getStatus()!=0){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }
        product.setStatus(1);
        productMapper.updateByPrimaryKey(product);
    }

    //从回收站中回复数据
    @SneakyThrows
    @Override
    public void back(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectByPrimaryKey(product);
        if (product1.getStatus()!=1){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }
        product1.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product1);
    }

    //删除
    @Override
    public void delete(Long id) throws BusinessException {
        Product t = new Product();
        t.setId(id);
        Product product = productMapper.selectByPrimaryKey(t);
        //判断是否后值
        if (product==null){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_NOT_FOUND);
        }
        //只有状态为回收站中或审核未通过是可以删除
        if (product.getStatus()!=1&&product.getStatus()!=2){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }
        productMapper.deleteByPrimaryKey(product);
    }

    //审核
    @Override
    public void publish(Long id) throws BusinessException {
        Product t = new Product();
        t.setId(id);
        Product product = productMapper.selectByPrimaryKey(t);
        if (product==null){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_NOT_FOUND);
        }
        if (product.getStatus()!=2){
            throw new BusinessException(BusinessCodeEnum.PRODUCT_STATUS_ERROR);
        }
        product.setStatus(0);
        productMapper.updateByPrimaryKeySelective(product);
    }
}
